package com.carkeeper.discover.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.discover.service.BlogCategoryService;
import com.carkeeper.pojo.BlogCategory;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
@CrossOrigin
@Api(tags = "文稿分类管理接口api",description = "文稿分类管理管理接口，提供页面的增、删、改、查")

public class BlogCategoryController {
    @Autowired
    private BlogCategoryService blogCategoryService;

    /**
     * 分页查询所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    @ApiOperation(value = "findPage",notes = "查看文稿分类列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示的记录数",required = false,paramType = "query")
    })
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum ,@RequestParam(defaultValue = "5")Integer pageSize){
      PageInfo<BlogCategory> info= blogCategoryService.findPage(pageNum,pageSize);
      return new Result(true,StatusCode.OK,"查询成功",info);
    }
    /**
     * 添加
     *
     * @param blogCategory
     * @return
     */
    @PostMapping
    @ApiOperation(value = "add",notes = "添加文稿分类",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "分类名称",required = true,paramType = "body"),
            @ApiImplicitParam(name = "description",value = "分类描述",required = true,paramType = "body")
    })
    public Result add(@RequestBody BlogCategory blogCategory) {

        blogCategoryService.add(blogCategory);
        return new Result(true, StatusCode.OK, "添加成功");
    }
    /**
     * 编辑
     * @param blogCategory
     * @return
     */
    @PutMapping
    @ApiOperation(value = "update",notes = "编辑文稿分类",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "分类名称",required = false,paramType = "body"),
            @ApiImplicitParam(name = "description",value = "分类描述",required = false,paramType = "body"),
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "body")
    })
    public Result update(@RequestBody BlogCategory blogCategory) {

        blogCategoryService.update(blogCategory);
        return new Result(true, StatusCode.OK, "编辑成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "delete",notes = "删除文稿分类",httpMethod = "Delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "path")
    })
    public Result delete(@PathVariable Integer id){
        try {
            blogCategoryService.delete(id);
            return new Result(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }
}
